package com.ccjq.jq173.member.dao.impl;

import com.ccjq.jq173.core.util.DbConnection;
import com.ccjq.jq173.core.util.UuidUtils;
import com.ccjq.jq173.member.dao.userdao;
import com.ccjq.jq173.member.po.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements userdao {
    @Override
    public User findUser_byname_and_password(String username, String password) {
        String sql="select * from t_user where username=? and password=?";
        User u=null;

        Connection conn=null;
        PreparedStatement st=null;
        ResultSet rs=null;

        try
        {
            conn= DbConnection.getConnection();
            st=conn.prepareStatement(sql);
            st.setString(1,username);
            st.setString(2,password);
            rs=st.executeQuery();
            if(rs.next())
            {
                u=new User();
                u.setUserId(rs.getString(1));
                u.setUserName(rs.getString(2));
                u.setPassWord(rs.getString(3));
                u.setMobilePhone(rs.getString(4));
            }
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(rs,st,conn);
        }
        return u;
    }

    @Override
    public int saveUser(String username, String password, String mobilePhone) {
        String sql="insert into t_user values(?,?,?,?)";
        String userid= UuidUtils.generateShortUUID("U",10);
        int result=0;

        Connection conn=null;
        PreparedStatement st=null;

        try {
            conn=DbConnection.getConnection();
            st=conn.prepareStatement(sql);
            st.setString(1,userid);
            st.setString(2,username);
            st.setString(3,password);
            st.setString(4,mobilePhone);
            result=st.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally
        {
            DbConnection.close(null,st,conn);
        }
        return result;
    }

    @Override
    public User findUserByName(String username) {
        String sql="select * from t_user where username=?";
        User u=null;

        Connection conn=null;
        PreparedStatement st=null;
        ResultSet rs=null;

        try
        {
            conn= DbConnection.getConnection();
            st=conn.prepareStatement(sql);
            st.setString(1,username);
            rs=st.executeQuery();
            if(rs.next())
            {
                u=new User();
                u.setUserId(rs.getString(1));
                u.setUserName(rs.getString(2));
                u.setPassWord(rs.getString(3));
                u.setMobilePhone(rs.getString(4));
            }
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(rs,st,conn);
        }
        return u;
    }
}
